/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.domain.Cabinet;
import me.zhengjie.modules.biz.domain.HazardousChemicals;
import me.zhengjie.modules.biz.domain.HazardousChemicalsTotal;
import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.repository.CabinetRepository;
import me.zhengjie.modules.biz.repository.HazardousChemicalsRepository;
import me.zhengjie.modules.biz.repository.HazardousChemicalsTotalRepository;
import me.zhengjie.modules.biz.repository.LabRepository;
import me.zhengjie.modules.biz.service.HazardousChemicalsTotalService;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsTotalDto;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsTotalQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.HazardousChemicalsMapper;
import me.zhengjie.modules.biz.service.mapstruct.HazardousChemicalsTotalMapper;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import me.zhengjie.utils.ValidationUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Admin
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-03-10
 **/
@Service
@RequiredArgsConstructor
public class HazardousChemicalsTotalServiceImpl implements HazardousChemicalsTotalService {

    private final HazardousChemicalsTotalRepository hazardousChemicalsTotalRepository;
    private final HazardousChemicalsTotalMapper hazardousChemicalsTotalMapper;

    private final LabRepository labRepository;
    private final CabinetRepository cabinetRepository;
    private final HazardousChemicalsMapper hazardousChemicalsMapper;
    private final HazardousChemicalsRepository hazardousChemicalsRepository;

    @Override
    public Map<String, Object> queryAll(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        Page<HazardousChemicalsTotal> page = hazardousChemicalsTotalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(hazardousChemicalsTotalMapper::toDto));

    }

    @Override
    public List<HazardousChemicalsTotalDto> queryAll(HazardousChemicalsTotalQueryCriteria criteria) {
        return hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public HazardousChemicalsTotalDto findById(Integer id) {
        HazardousChemicalsTotal hazardousChemicalsTotal = hazardousChemicalsTotalRepository.findById(id).orElseGet(HazardousChemicalsTotal::new);
        ValidationUtil.isNull(hazardousChemicalsTotal.getId(), "HazardousChemicalsTotal", "id", id);
        return hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotal);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HazardousChemicalsTotalDto create(HazardousChemicalsTotal resources) {
        return hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(HazardousChemicalsTotal resources) {
        HazardousChemicalsTotal hazardousChemicalsTotal = hazardousChemicalsTotalRepository.findById(resources.getId()).orElseGet(HazardousChemicalsTotal::new);
        ValidationUtil.isNull(hazardousChemicalsTotal.getId(), "HazardousChemicalsTotal", "id", resources.getId());
        hazardousChemicalsTotal.copy(resources);
        hazardousChemicalsTotalRepository.save(hazardousChemicalsTotal);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            hazardousChemicalsTotalRepository.deleteById(id);
        }
    }

    @Override
    public void downloadHazardousChemicalsTotal(List<Map<String, Object>> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        JSONArray jsonArray;

        for (Map<String, Object> hazardousChemicalsTotal : all) {
            jsonArray = JSONUtil.parseArray(hazardousChemicalsTotal.get("成分/名称"));
            for (int i = 0; i < jsonArray.size(); i++) {
                Map<String, Object> map = new LinkedHashMap<>();
                map.put("标准物质名称", hazardousChemicalsTotal.get("标准物质名称"));
                map.put("标准物质编号", hazardousChemicalsTotal.get("标准物质编号"));
                map.put("批号", hazardousChemicalsTotal.get("批号"));
                map.put("成分/名称", jsonArray.getJSONObject(i).getStr("ingredientName"));
                map.put("标准值(μg/mL)", jsonArray.getJSONObject(i).getStr("value"));
                map.put("不确定度", jsonArray.getJSONObject(i).getStr("uncertainty"));
                String unit = hazardousChemicalsTotal.get("单位").equals("1") ? "支" : "ml";
                map.put("数量", hazardousChemicalsTotal.get("数量") + unit);
                map.put("有效期", hazardousChemicalsTotal.get("有效期"));
                list.add(map);
            }
        }
        FileUtil.downloadExcelFH(list, response);
    }

    @Override
    public Map<String, Object> queryAllByRequest(HttpServletRequest request) {

        if (request.getParameter("id") != null) {
            HazardousChemicalsTotal hazardousChemicalsTotal = hazardousChemicalsTotalRepository.getById(Integer.parseInt(request.getParameter("id")));
            HazardousChemicalsTotalDto hazardousChemicalsTotalDto = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotal);
            hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
            Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
            Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
            if (lab.getName() != null) {
                hazardousChemicalsTotalDto.setLabName(lab.getName());
            } else {
                hazardousChemicalsTotalDto.setLabName("-");
            }
            if (cabinet.getName() != null) {
                hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
            } else {
                hazardousChemicalsTotalDto.setCabinetName("-");
            }
            hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
            Map<String, Object> hazardousChemicalsTotalMap = new HashMap<String, Object>();
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDto);
            hazardousChemicalsTotalMap.put("totalElements", 1);
            return hazardousChemicalsTotalMap;
        }


        int type = Integer.parseInt(request.getParameter("type"));
        int page = Integer.parseInt(request.getParameter("page"));
        int size = Integer.parseInt(request.getParameter("size"));
        int real_page = page * size;
        int real_size = real_page + size;


        if (type == 1) {
            Map<String, Object> hazardousChemicalsTotalMap = new HashMap<String, Object>();


            if (request.getParameter("name") == null && request.getParameter("labId") == null && request.getParameter("cabinetId") == null && request.getParameter("category") == null) {
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNull(type, real_page, real_size);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNull(type, 0, 1000000000);

                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }

            if (request.getParameter("name") != null && request.getParameter("labId") == null && request.getParameter("cabinetId") == null && request.getParameter("category") == null) {
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByName(type, request.getParameter("name"), real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByName(type, request.getParameter("name"), 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());

                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
            if (request.getParameter("name") == null && request.getParameter("labId") == null && request.getParameter("cabinetId") == null && request.getParameter("category") != null) {
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByCategory(type, request.getParameter("category"), real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByCategory(type, request.getParameter("category"), 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
            if (request.getParameter("name") == null && request.getParameter("labId") != null && request.getParameter("cabinetId") == null) {
                int labId = Integer.parseInt(request.getParameter("labId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByLabId(type, labId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByLabId(type, labId, 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
            if (request.getParameter("name") != null && request.getParameter("labId") == null && request.getParameter("cabinetId") == null && request.getParameter("category") != null) {
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameCategory(type, request.getParameter("name"), request.getParameter("category"), real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameCategory(type, request.getParameter("name"), request.getParameter("category"), 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
            if (request.getParameter("name") == null && request.getParameter("labId") == null && request.getParameter("cabinetId") != null) {
                int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByCabinetId(type, cabinetId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByCabinetId(type, cabinetId, 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }

                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }

            if (request.getParameter("name") != null && request.getParameter("labId") != null && request.getParameter("cabinetId") == null) {
                int labId = Integer.parseInt(request.getParameter("labId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLabid(type, request.getParameter("name"), labId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLabid(type, request.getParameter("name"), labId, 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
            if (request.getParameter("name") == null && request.getParameter("labId") != null && request.getParameter("cabinetId") != null) {
                int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
                int labId = Integer.parseInt(request.getParameter("labId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByLabIdcabinetId(type, labId, cabinetId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByLabIdcabinetId(type, labId, cabinetId, 0, 1000000000);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
            if (request.getParameter("name") != null && request.getParameter("labId") == null && request.getParameter("cabinetId") != null) {
                int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLcabinetId(type, request.getParameter("name"), cabinetId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLcabinetId(type, request.getParameter("name"), cabinetId, 0, 1000000000);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }

            if (request.getParameter("name") != null && request.getParameter("labId") != null && request.getParameter("cabinetId") != null) {
                int labId = Integer.parseInt(request.getParameter("labId"));
                int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLabIdLcabinetId(type, request.getParameter("name"), labId, cabinetId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLabIdLcabinetId(type, request.getParameter("name"), labId, cabinetId, 0, 1000000000);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
        } else if (type == 2) {
            Map<String, Object> hazardousChemicalsTotalMap = new HashMap<String, Object>();
            if (request.getParameter("name") == null && request.getParameter("labId") == null && request.getParameter("cabinetId") == null && request.getParameter("category") == null) {
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNullAndBatchNumber(type, real_page, real_size);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNullAndBatchNumber(type, 0, 1000000000);

                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNumAndBatchNumber(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getBatchNumber()));

                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }

            if (request.getParameter("name") != null && request.getParameter("labId") == null && request.getParameter("cabinetId") == null && request.getParameter("category") == null) {
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameAndBatchNumber(type, request.getParameter("name"), real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameAndBatchNumber(type, request.getParameter("name"), 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNumAndBatchNumber(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getBatchNumber()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());

                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
//            if (request.getParameter("name")==null&&request.getParameter("labId")==null&&request.getParameter("cabinetId")==null&& request.getParameter("category")!=null){
//                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByCategory(type,request.getParameter("category"),real_page,real_size);
//                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByCategory(type,request.getParameter("category"),0,1000000000);
//
//                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
//                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto: hazardousChemicalsTotalDtoList) {
//                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
//                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
//                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
//                    if (lab!=null){
//                        hazardousChemicalsTotalDto.setLabName(lab.getName());
//                    }else {
//                        hazardousChemicalsTotalDto.setLabName("-");
//                    }
//                    if (cabinet!=null){
//                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
//                    }else {
//                        hazardousChemicalsTotalDto.setCabinetName("-");
//                    }
//                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(),hazardousChemicalsTotalDto.getCountType()));
//                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
//                }
//                hazardousChemicalsTotalMap.put("content",hazardousChemicalsTotalDtoList);
//                hazardousChemicalsTotalMap.put("totalElements",listCount.size());
//                return hazardousChemicalsTotalMap;
//            }
            if (request.getParameter("name") == null && request.getParameter("labId") != null && request.getParameter("cabinetId") == null) {
                int labId = Integer.parseInt(request.getParameter("labId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByLabIdAndBatchNumber(type, labId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByLabIdAndBatchNumber(type, labId, 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNumAndBatchNumber(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getBatchNumber()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
//            if (request.getParameter("name")!=null&&request.getParameter("labId")==null&&request.getParameter("cabinetId")==null&& request.getParameter("category")!=null){
//                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameCategory(type,request.getParameter("name"),request.getParameter("category"),real_page,real_size);
//                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameCategory(type,request.getParameter("name"),request.getParameter("category"),0,1000000000);
//
//                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
//                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto: hazardousChemicalsTotalDtoList) {
//                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
//                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
//                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
//                    if (lab!=null){
//                        hazardousChemicalsTotalDto.setLabName(lab.getName());
//                    }else {
//                        hazardousChemicalsTotalDto.setLabName("-");
//                    }
//                    if (cabinet!=null){
//                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
//                    }else {
//                        hazardousChemicalsTotalDto.setCabinetName("-");
//                    }
//                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(),hazardousChemicalsTotalDto.getCountType()));
//                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
//                }
//                hazardousChemicalsTotalMap.put("content",hazardousChemicalsTotalDtoList);
//                hazardousChemicalsTotalMap.put("totalElements",listCount.size());
//                return hazardousChemicalsTotalMap;
//            }
            if (request.getParameter("name") == null && request.getParameter("labId") == null && request.getParameter("cabinetId") != null) {
                int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByCabinetIdAndBatchNumber(type, cabinetId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByCabinetIdAndBatchNumber(type, cabinetId, 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNumAndBatchNumber(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getBatchNumber()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }

                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }

            if (request.getParameter("name") != null && request.getParameter("labId") != null && request.getParameter("cabinetId") == null) {
                int labId = Integer.parseInt(request.getParameter("labId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLabidAndBatchNumber(type, request.getParameter("name"), labId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLabidAndBatchNumber(type, request.getParameter("name"), labId, 0, 1000000000);

                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNumAndBatchNumber(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getBatchNumber()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
            if (request.getParameter("name") == null && request.getParameter("labId") != null && request.getParameter("cabinetId") != null) {
                int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
                int labId = Integer.parseInt(request.getParameter("labId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByLabIdcabinetIdAndBatchNumber(type, labId, cabinetId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByLabIdcabinetIdAndBatchNumber(type, labId, cabinetId, 0, 1000000000);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNumAndBatchNumber(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getBatchNumber()));
                    hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
            if (request.getParameter("name") != null && request.getParameter("labId") == null && request.getParameter("cabinetId") != null) {
                int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLcabinetIdAndBatchNumber(type, request.getParameter("name"), cabinetId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLcabinetIdAndBatchNumber(type, request.getParameter("name"), cabinetId, 0, 1000000000);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNumAndBatchNumber(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getBatchNumber()));
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }

            if (request.getParameter("name") != null && request.getParameter("labId") != null && request.getParameter("cabinetId") != null) {
                int labId = Integer.parseInt(request.getParameter("labId"));
                int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLabIdLcabinetIdAndBatchNumber(type, request.getParameter("name"), labId, cabinetId, real_page, real_size);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLabIdLcabinetIdAndBatchNumber(type, request.getParameter("name"), labId, cabinetId, 0, 1000000000);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNumAndBatchNumber(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getBatchNumber()));
                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            }
        }

        Map<String, Object> hazardousChemicalsTotalMap = new HashMap<String, Object>();
        if (request.getParameter("name") == null && request.getParameter("labId") == null && request.getParameter("cabinetId") == null) {
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNull(type, real_page, real_size);
            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNull(type, 0, 1000000000);

            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        }

        if (request.getParameter("name") != null && request.getParameter("labId") == null && request.getParameter("cabinetId") == null) {
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByName(type, request.getParameter("name"), real_page, real_size);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByName(type, request.getParameter("name"), 0, 1000000000);

            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());

                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        }
        if (request.getParameter("name") == null && request.getParameter("labId") != null && request.getParameter("cabinetId") == null) {
            int labId = Integer.parseInt(request.getParameter("labId"));
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByLabId(type, labId, real_page, real_size);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByLabId(type, labId, 0, 1000000000);

            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        }
        if (request.getParameter("name") == null && request.getParameter("labId") == null && request.getParameter("cabinetId") != null) {
            int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByCabinetId(type, cabinetId, real_page, real_size);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByCabinetId(type, cabinetId, 0, 1000000000);

            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
            }

            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        }

        if (request.getParameter("name") != null && request.getParameter("labId") != null && request.getParameter("cabinetId") == null) {
            int labId = Integer.parseInt(request.getParameter("labId"));
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLabid(type, request.getParameter("name"), labId, real_page, real_size);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLabid(type, request.getParameter("name"), labId, 0, 1000000000);

            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        }
        if (request.getParameter("name") == null && request.getParameter("labId") != null && request.getParameter("cabinetId") != null) {
            int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
            int labId = Integer.parseInt(request.getParameter("labId"));
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByLabIdcabinetId(type, labId, cabinetId, real_page, real_size);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByLabIdcabinetId(type, labId, cabinetId, 0, 1000000000);
            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        }
        if (request.getParameter("name") != null && request.getParameter("labId") == null && request.getParameter("cabinetId") != null) {
            int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLcabinetId(type, request.getParameter("name"), cabinetId, real_page, real_size);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLcabinetId(type, request.getParameter("name"), cabinetId, 0, 1000000000);
            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        }

        if (request.getParameter("name") != null && request.getParameter("labId") != null && request.getParameter("cabinetId") != null) {
            int labId = Integer.parseInt(request.getParameter("labId"));
            int cabinetId = Integer.parseInt(request.getParameter("cabinetId"));
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameLabIdLcabinetId(type, request.getParameter("name"), labId, cabinetId, real_page, real_size);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameLabIdLcabinetId(type, request.getParameter("name"), labId, cabinetId, 0, 1000000000);
            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));
                hazardousChemicalsTotalDto.setAmount(hazardousChemicalsTotalDto.getTotalNumber());
            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        }
        return hazardousChemicalsTotalMap;
    }

    @Override
    public Map<String, Object> statisticsList(HttpServletRequest request) {
        int page = Integer.parseInt(request.getParameter("page"));
        int size = Integer.parseInt(request.getParameter("size"));
        int real_page = page * size;
        int real_size = real_page + size;
//        return null;
        Map<String, Object> hazardousChemicalsTotalMap = new HashMap<String, Object>();
        if (request.getParameter("name") == null && request.getParameter("cabinetIds") == null && request.getParameter("startTime") == null) {
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNullNotType(real_page, real_size);
            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNullNotType(0, 1000000000);

            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

            }

            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        } else if (request.getParameter("name") != null && request.getParameter("cabinetIds") == null && request.getParameter("startTime") == null) {
            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameNotType(request.getParameter("name"), real_page, real_size);
            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameNotType(request.getParameter("name"), 0, 1000000000);

            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        } else if (request.getParameter("name") == null && request.getParameter("cabinetIds") != null && request.getParameter("startTime") == null) {
            String[] cabinetList = request.getParameter("cabinetIds").split(",");
            int[] cabinetIdList = new int[cabinetList.length];
            for (int i = 0; i < cabinetList.length; i++) {
                cabinetIdList[i] = Integer.parseInt(cabinetList[i]);
            }

            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByCabinetINotType(cabinetIdList, real_page, real_size);
            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByCabinetINotType(cabinetIdList, 0, 1000000000);

            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        } else if (request.getParameter("name") == null && request.getParameter("cabinetIds") == null && request.getParameter("startTime") != null) {
            String early = request.getParameter("startTime");
            String night = request.getParameter("endTime");
            try {
                Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateFormatUtils.format(new Date(Long.parseLong(early)), "yyyy-MM-dd HH:mm:ss"));
                Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateFormatUtils.format(new Date(Long.parseLong(night)), "yyyy-MM-dd HH:mm:ss"));
                Timestamp ts = new Timestamp(startTime.getTime());
                Timestamp te = new Timestamp(endTime.getTime());
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByTimeNotType(ts, te, real_page, real_size);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByTimeNotType(ts, te, 0, 1000000000);

                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        } else if (request.getParameter("name") != null && request.getParameter("cabinetIds") != null && request.getParameter("startTime") == null) {

            String[] cabinetList = request.getParameter("cabinetIds").split(",");
            int[] cabinetIdList = new int[cabinetList.length];
            for (int i = 0; i < cabinetList.length; i++) {
                cabinetIdList[i] = Integer.parseInt(cabinetList[i]);
            }


            List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameAndCabinetIdNotType(request.getParameter("name"), cabinetIdList, real_page, real_size);
            List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
            List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameAndCabinetIdNotType(request.getParameter("name"), cabinetIdList, 0, 1000000000);

            for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (lab != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                } else {
                    hazardousChemicalsTotalDto.setLabName("-");
                }
                if (cabinet != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                } else {
                    hazardousChemicalsTotalDto.setCabinetName("-");
                }
                hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

            }
            hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
            hazardousChemicalsTotalMap.put("totalElements", listCount.size());
            return hazardousChemicalsTotalMap;
        } else if (request.getParameter("name") != null && request.getParameter("cabinetIds") == null && request.getParameter("startTime") != null) {
            String early = request.getParameter("startTime");
            String night = request.getParameter("endTime");
            try {
                Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateFormatUtils.format(new Date(Long.parseLong(early)), "yyyy-MM-dd HH:mm:ss"));
                Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateFormatUtils.format(new Date(Long.parseLong(night)), "yyyy-MM-dd HH:mm:ss"));
                Timestamp ts = new Timestamp(startTime.getTime());
                Timestamp te = new Timestamp(endTime.getTime());
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameAndTimeNotType(request.getParameter("name"), ts, te, real_page, real_size);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameAndTimeNotType(request.getParameter("name"), ts, te, 0, 1000000000);

                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        } else if (request.getParameter("name") == null && request.getParameter("cabinetIds") != null && request.getParameter("startTime") != null) {

            String[] cabinetList = request.getParameter("cabinetIds").split(",");
            int[] cabinetIdList = new int[cabinetList.length];
            for (int i = 0; i < cabinetList.length; i++) {
                cabinetIdList[i] = Integer.parseInt(cabinetList[i]);
            }


            String early = request.getParameter("startTime");
            String night = request.getParameter("endTime");
            try {
                Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateFormatUtils.format(new Date(Long.parseLong(early)), "yyyy-MM-dd HH:mm:ss"));
                Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateFormatUtils.format(new Date(Long.parseLong(night)), "yyyy-MM-dd HH:mm:ss"));
                Timestamp ts = new Timestamp(startTime.getTime());
                Timestamp te = new Timestamp(endTime.getTime());
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByCabinetIdAndTimeNotType(cabinetIdList, ts, te, real_page, real_size);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByCabinetIdAndTimeNotType(cabinetIdList, ts, te, 0, 1000000000);

                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        } else if (request.getParameter("name") != null && request.getParameter("cabinetIds") != null && request.getParameter("startTime") != null) {

            String[] cabinetList = request.getParameter("cabinetIds").split(",");
            int[] cabinetIdList = new int[cabinetList.length];
            for (int i = 0; i < cabinetList.length; i++) {
                cabinetIdList[i] = Integer.parseInt(cabinetList[i]);
            }


            String early = request.getParameter("startTime");
            String night = request.getParameter("endTime");
            try {
                Date startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateFormatUtils.format(new Date(Long.parseLong(early)), "yyyy-MM-dd HH:mm:ss"));
                Date endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(DateFormatUtils.format(new Date(Long.parseLong(night)), "yyyy-MM-dd HH:mm:ss"));
                Timestamp ts = new Timestamp(startTime.getTime());
                Timestamp te = new Timestamp(endTime.getTime());
                List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNameAndCabinetIdAndTimeNotType(request.getParameter("name"), cabinetIdList, ts, te, real_page, real_size);
                List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
                List<HazardousChemicalsTotal> listCount = hazardousChemicalsTotalRepository.queryAllByNameAndCabinetIdAndTimeNotType(request.getParameter("name"), cabinetIdList, ts, te, 0, 1000000000);

                for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalDto.getAmount());
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                    if (lab != null) {
                        hazardousChemicalsTotalDto.setLabName(lab.getName());
                    } else {
                        hazardousChemicalsTotalDto.setLabName("-");
                    }
                    if (cabinet != null) {
                        hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                    } else {
                        hazardousChemicalsTotalDto.setCabinetName("-");
                    }
                    hazardousChemicalsTotalDto.setTotalNumber(hazardousChemicalsTotalRepository.sumTotalNum(hazardousChemicalsTotalDto.getName(), hazardousChemicalsTotalDto.getType()));

                }
                hazardousChemicalsTotalMap.put("content", hazardousChemicalsTotalDtoList);
                hazardousChemicalsTotalMap.put("totalElements", listCount.size());
                return hazardousChemicalsTotalMap;
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }
        return null;
    }

    @Override
    public List<HazardousChemicalsTotalDto> exportDataTotal(HazardousChemicalsTotalQueryCriteria criteria) {
        Specification<HazardousChemicalsTotal> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (criteria.getName() != null) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + criteria.getName() + "%"));
            }
            if (criteria.getCategory() != null) {
                predicates.add(criteriaBuilder.equal(root.get("category"), criteria.getCategory()));
            }
            if (criteria.getType() != null) {
                predicates.add(criteriaBuilder.equal(root.get("type"), criteria.getType()));
            }
            if (criteria.getLabId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("labId"), criteria.getLabId()));
            }
            if (criteria.getCabinetId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("cabinetId"), criteria.getCabinetId()));
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };

        List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.findAll(specification);
        List<HazardousChemicalsTotalDto> hazardousChemicalsDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);

        return hazardousChemicalsDtoList;
    }

    @Override
    public void exportEquipmentTotal(List<HazardousChemicalsTotalDto> exportDataTotal, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : exportDataTotal) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("名称", hazardousChemicalsTotalDto.getName());
            String category = "";
            switch (hazardousChemicalsTotalDto.getCategory()) {
                case "1":
                    category = "玻璃器皿";
                    break;
                case "2":
                    category = "实验耗材";
                    break;
                case "3":
                    category = "采样工具";
                    break;
                case "4":
                    category = "仪器设备";
                    break;
            }
            map.put("类型", category);
            String unit = "";
            switch (hazardousChemicalsTotalDto.getUnit()) {
                case 1:
                    unit = "个";
                    break;
                case 2:
                    unit = "台";
                    break;
                case 3:
                    unit = "套";
                    break;
            }
            map.put("总量", hazardousChemicalsTotalDto.getAmount() + unit);
            String storage = "";
            switch (hazardousChemicalsTotalDto.getStorage()) {
                case 1:
                    storage = "器材室";
                    break;
                case 2:
                    storage = "备品室";
                    break;
                case 3:
                    storage = "外业仓库";
                    break;
            }
            map.put("仓库", storage);

            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void exportChemicalTotal(List<HazardousChemicalsTotalDto> exportDataTotal, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : exportDataTotal) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("名称", hazardousChemicalsTotalDto.getName());
            map.put("级别", hazardousChemicalsTotalDto.getGrade());
            map.put("总量", hazardousChemicalsTotalDto.getTotalNumber() + "ml");
            String dangerClasses = null;
            switch (hazardousChemicalsTotalDto.getDangerClasses()) {
                case 1:
                    dangerClasses = "一般试剂（I）";
                    break;
                case 2:
                    dangerClasses = "有机试剂（II）";
                    break;
                case 3:
                    dangerClasses = "剧毒试剂/易致毒试剂（III）";
                    break;
            }
            map.put("危险等级", dangerClasses);
            Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
            map.put("实验室", lab.getName());

            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> equipmentTotalList(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        Specification<HazardousChemicalsTotal> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (criteria.getName() != null) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + criteria.getName() + "%"));
            }
            if (criteria.getCategory() != null) {
                predicates.add(criteriaBuilder.equal(root.get("category"), criteria.getCategory()));
            }
            if (criteria.getCabinetId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("cabinetId"), criteria.getCabinetId()));
            }
            predicates.add(criteriaBuilder.equal(root.get("type"), 1));
            query.where(predicates.toArray(new Predicate[0]));
            query.orderBy(criteriaBuilder.asc(root.get("name")));

            return query.getRestriction();
        };
        Page<HazardousChemicalsTotal> page = hazardousChemicalsTotalRepository.findAll(specification, pageable);
        Page<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoPage = page.map(hazardousChemicalsTotalMapper::toDto);
        long pageSize = page.getTotalElements();
        List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalDtoPage.getContent();
        List<HazardousChemicalsTotalDto> newhazardousChemicalsTotalDtoList = new ArrayList<>();
        if (!hazardousChemicalsTotalDtoList.isEmpty()) {
            if (hazardousChemicalsTotalDtoList.get(0).getCabinetId() != null) {
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDtoList.get(0).getCabinetId()).orElseGet(Cabinet::new);
                if (cabinet.getId() != null) {
                    hazardousChemicalsTotalDtoList.get(0).setCabinetName(cabinet.getName());
                }
            }
            newhazardousChemicalsTotalDtoList.add(hazardousChemicalsTotalDtoList.get(0));
        }

        int j = 0;
        for (int i = 1; i < hazardousChemicalsTotalDtoList.size(); i++) {
            if (hazardousChemicalsTotalDtoList.get(i).getName().equals(hazardousChemicalsTotalDtoList.get(i - 1).getName()) && hazardousChemicalsTotalDtoList.get(i).getStorage().equals(hazardousChemicalsTotalDtoList.get(i - 1).getStorage())) {
                newhazardousChemicalsTotalDtoList.get(j).setTotalNumber(newhazardousChemicalsTotalDtoList.get(j).getTotalNumber() + hazardousChemicalsTotalDtoList.get(i).getTotalNumber());
                pageSize--;
            } else {
                if (hazardousChemicalsTotalDtoList.get(i).getCabinetId() != null) {
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDtoList.get(i).getCabinetId()).orElseGet(Cabinet::new);
                    if (cabinet.getId() != null) {
                        hazardousChemicalsTotalDtoList.get(i).setCabinetName(cabinet.getName());
                    }
                }
                newhazardousChemicalsTotalDtoList.add(hazardousChemicalsTotalDtoList.get(i));
                j++;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("content", newhazardousChemicalsTotalDtoList);
        map.put("totalElements", pageSize);
        return map;
    }

    @Override
    public Map<String, Object> hazardousChemicalsTotalList(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        Specification<HazardousChemicalsTotal> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (criteria.getName() != null) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + criteria.getName() + "%"));
            }
            if (criteria.getCabinetId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("cabinetId"), criteria.getCabinetId()));
            }
            if (criteria.getLabId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("labId"), criteria.getLabId()));
            }
            if (criteria.getCreatedAt() != null) {
                predicates.add(criteriaBuilder.between(root.get("createdAt"), criteria.getCreatedAt().get(0), criteria.getCreatedAt().get(1)));
            }
            predicates.add(criteriaBuilder.equal(root.get("type"), 2));
            query.where(predicates.toArray(new Predicate[0]));
            query.orderBy(criteriaBuilder.asc(root.get("name")), criteriaBuilder.asc(root.get("batchNumber")), criteriaBuilder.asc(root.get("cabinetId")));

            return query.getRestriction();
        };
        Page<HazardousChemicalsTotal> page = hazardousChemicalsTotalRepository.findAll(specification, pageable);
        Page<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoPage = page.map(hazardousChemicalsTotalMapper::toDto);
        long pageSize = page.getTotalElements();
        List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalDtoPage.getContent();
        List<HazardousChemicalsTotalDto> newhazardousChemicalsTotalDtoList = new ArrayList<>();
        if (!hazardousChemicalsTotalDtoList.isEmpty()) {
            if (hazardousChemicalsTotalDtoList.get(0).getLabId() != null) {
                Lab lab = labRepository.findById(hazardousChemicalsTotalDtoList.get(0).getLabId()).orElseGet(Lab::new);
                if (lab.getId() != null) {
                    hazardousChemicalsTotalDtoList.get(0).setLabName(lab.getName());
                }
            }
            if (hazardousChemicalsTotalDtoList.get(0).getCabinetId() != null) {
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDtoList.get(0).getCabinetId()).orElseGet(Cabinet::new);
                if (cabinet.getId() != null) {
                    hazardousChemicalsTotalDtoList.get(0).setCabinetName(cabinet.getName());
                }
            }
            newhazardousChemicalsTotalDtoList.add(hazardousChemicalsTotalDtoList.get(0));
        }
        int j = 0;
        for (int i = 1; i < hazardousChemicalsTotalDtoList.size(); i++) {
            if (hazardousChemicalsTotalDtoList.get(i).getName().equals(hazardousChemicalsTotalDtoList.get(i - 1).getName()) && hazardousChemicalsTotalDtoList.get(i).getBatchNumber().equals(hazardousChemicalsTotalDtoList.get(i - 1).getBatchNumber()) && hazardousChemicalsTotalDtoList.get(i).getCabinetId().equals(hazardousChemicalsTotalDtoList.get(i - 1).getCabinetId()) && hazardousChemicalsTotalDtoList.get(i).getExpirationDate().equals(hazardousChemicalsTotalDtoList.get(i - 1).getExpirationDate())) {
                newhazardousChemicalsTotalDtoList.get(j).setTotalNumber(newhazardousChemicalsTotalDtoList.get(j).getTotalNumber() + hazardousChemicalsTotalDtoList.get(i).getTotalNumber());
                pageSize--;
            } else {
                if (hazardousChemicalsTotalDtoList.get(i).getLabId() != null) {
                    Lab lab = labRepository.findById(hazardousChemicalsTotalDtoList.get(i).getLabId()).orElseGet(Lab::new);
                    if (lab.getId() != null) {
                        hazardousChemicalsTotalDtoList.get(i).setLabName(lab.getName());
                    }
                }
                if (hazardousChemicalsTotalDtoList.get(i).getCabinetId() != null) {
                    Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDtoList.get(i).getCabinetId()).orElseGet(Cabinet::new);
                    if (cabinet.getId() != null) {
                        hazardousChemicalsTotalDtoList.get(i).setCabinetName(cabinet.getName());
                    }
                }
                newhazardousChemicalsTotalDtoList.add(hazardousChemicalsTotalDtoList.get(i));
                j++;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("content", newhazardousChemicalsTotalDtoList);
        map.put("totalElements", pageSize);
        return map;
    }

    @Override
    public Map<String, Object> chemicalTotalList(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        Specification<HazardousChemicalsTotal> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (criteria.getName() != null) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + criteria.getName() + "%"));
            }
            if (criteria.getLabId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("labId"), criteria.getLabId()));
            }
            if (criteria.getCabinetId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("cabinetId"), criteria.getCabinetId()));
            }
            if (criteria.getDangerClasses() != null) {
                predicates.add(criteriaBuilder.equal(root.get("dangerClasses"), criteria.getDangerClasses()));
            }
            if (criteria.getCreatedAt() != null) {
                predicates.add(criteriaBuilder.between(root.get("createdAt"), criteria.getCreatedAt().get(0), criteria.getCreatedAt().get(1)));
            }
            predicates.add(criteriaBuilder.notEqual(root.get("totalNumber"), 0f));
            predicates.add(criteriaBuilder.equal(root.get("type"), 3));
            query.where(predicates.toArray(new Predicate[0]));
            query.orderBy(criteriaBuilder.asc(root.get("name")), criteriaBuilder.asc(root.get("labId")), criteriaBuilder.asc(root.get("cabinetId")));

            return query.getRestriction();
        };
        Page<HazardousChemicalsTotal> page = hazardousChemicalsTotalRepository.findAll(specification, pageable);
        Page<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoPage = page.map(hazardousChemicalsTotalMapper::toDto);
        long pageSize = page.getTotalElements();
        List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalDtoPage.getContent();
        List<HazardousChemicalsTotalDto> newhazardousChemicalsTotalDtoList = new ArrayList<>();
        if (hazardousChemicalsTotalDtoList.size() == 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("content", newhazardousChemicalsTotalDtoList);
            map.put("totalElements", pageSize);
            return map;
        }

        newhazardousChemicalsTotalDtoList.add(hazardousChemicalsTotalDtoList.get(0));
        int j = 0;
        for (int i = 1; i < hazardousChemicalsTotalDtoList.size(); i++) {
            if (hazardousChemicalsTotalDtoList.get(i).getName().equals(hazardousChemicalsTotalDtoList.get(i - 1).getName()) && hazardousChemicalsTotalDtoList.get(i).getCabinetId().equals(hazardousChemicalsTotalDtoList.get(i - 1).getCabinetId()) && hazardousChemicalsTotalDtoList.get(i).getExpirationDate().equals(hazardousChemicalsTotalDtoList.get(i - 1).getExpirationDate())) {
                newhazardousChemicalsTotalDtoList.get(j).setTotalNumber(newhazardousChemicalsTotalDtoList.get(j).getTotalNumber() + hazardousChemicalsTotalDtoList.get(i).getTotalNumber());
                pageSize--;
            } else {
                newhazardousChemicalsTotalDtoList.add(hazardousChemicalsTotalDtoList.get(i));
                j++;
            }
        }
        for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : newhazardousChemicalsTotalDtoList) {

            Integer bottleNum = hazardousChemicalsRepository.countBottle(hazardousChemicalsTotalDto.getName());
            hazardousChemicalsTotalDto.setBottle(bottleNum);
            if (hazardousChemicalsTotalDto.getLabId() != null) {
                Lab lab = labRepository.findById(hazardousChemicalsTotalDto.getLabId()).orElseGet(Lab::new);
                if (lab.getId() != null) {
                    hazardousChemicalsTotalDto.setLabName(lab.getName());
                }
            }
            if (hazardousChemicalsTotalDto.getCabinetId() != null) {
                Cabinet cabinet = cabinetRepository.findById(hazardousChemicalsTotalDto.getCabinetId()).orElseGet(Cabinet::new);
                if (cabinet.getId() != null) {
                    hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("content", newhazardousChemicalsTotalDtoList);
        map.put("totalElements", pageSize);
        return map;
    }

    @Override
    public String checkInventory(String name, List<String> codeList) {
        List<Integer> integers = new ArrayList<>();
        for (String s : codeList) {
            s = s.replace("h", "0");
            integers.add(Integer.valueOf(s));
        }
        for (Integer i : integers) {
            HazardousChemicals hazardousChemicals = hazardousChemicalsRepository.getById(i);
            if (!Objects.equals(hazardousChemicals.getName(), name)) {
                return "化学试剂不匹配";
            }
        }
        List<Integer> hazardousChemicalsIdList = hazardousChemicalsRepository.findNoDeliveryByName(name);
        if (hazardousChemicalsIdList.size() > integers.size()) {
            List<Integer> diff = getDiffrent(hazardousChemicalsIdList, integers);
            System.out.println(diff);
            return "盘盈";
        } else if (hazardousChemicalsIdList.size() < integers.size()) {
            List<Integer> diff = getDiffrent(hazardousChemicalsIdList, integers);
            System.out.println(diff);
            return "盘亏";
        } else {
            return "数量匹配";
        }
    }

    @Override
    public String checkInventorySolo(float num, String code) {
        code = code.replace("h", "0");
        Integer codeNum = Integer.valueOf(code);
        HazardousChemicals hazardousChemicals = hazardousChemicalsRepository.getById(codeNum);
        if (num > hazardousChemicals.getResidue()) {
            HazardousChemicalsTotal hazardousChemicalsTotal =
                    hazardousChemicalsTotalRepository.findByNameAndType(hazardousChemicals.getName(), hazardousChemicals.getType());

            hazardousChemicalsTotal.setTotalNumber(hazardousChemicalsTotal.getTotalNumber() - hazardousChemicals.getResidue() + num);
            hazardousChemicalsTotalRepository.save(hazardousChemicalsTotal);
            hazardousChemicals.setResidue(num);
            hazardousChemicalsRepository.save(hazardousChemicals);
            return "盘亏";
        } else if (num < hazardousChemicals.getResidue()) {
            HazardousChemicalsTotal hazardousChemicalsTotal =
                    hazardousChemicalsTotalRepository.findByNameAndType(hazardousChemicals.getName(), hazardousChemicals.getType());
            hazardousChemicalsTotal.setTotalNumber(hazardousChemicalsTotal.getTotalNumber() - hazardousChemicals.getResidue() + num);
            hazardousChemicalsTotalRepository.save(hazardousChemicalsTotal);
            hazardousChemicals.setResidue(num);
            hazardousChemicalsRepository.save(hazardousChemicals);
            return "盘盈";
        } else {
            return "容量匹配";
        }
    }

    @Override
    public Map<String, Object> statistics(HazardousChemicalsTotalQueryCriteria criteria, Pageable pageable) {
        Specification<HazardousChemicalsTotal> specification = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (criteria.getName() != null) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%" + criteria.getName() + "%"));
            }
            if (criteria.getLabId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("labId"), criteria.getLabId()));
            }
            if (criteria.getCabinetId() != null) {
                predicates.add(criteriaBuilder.equal(root.get("cabinetId"), criteria.getCabinetId()));
            }
            if (criteria.getDangerClasses() != null) {
                predicates.add(criteriaBuilder.equal(root.get("dangerClasses"), criteria.getDangerClasses()));
            }
            if (criteria.getCreatedAt() != null) {
                predicates.add(criteriaBuilder.between(root.get("createdAt"), criteria.getCreatedAt().get(0), criteria.getCreatedAt().get(1)));
            }
            if (criteria.getType() != null) {
                predicates.add(criteriaBuilder.equal(root.get("type"), criteria.getType()));

            }
            predicates.add(criteriaBuilder.notEqual(root.get("totalNumber"), 0f));
            query.where(predicates.toArray(new Predicate[predicates.size()]));
            query.orderBy(criteriaBuilder.asc(root.get("name")), criteriaBuilder.asc(root.get("labId")), criteriaBuilder.asc(root.get("cabinetId")));

            return query.getRestriction();
        };
        Page<HazardousChemicalsTotal> page = hazardousChemicalsTotalRepository.findAll(specification, pageable);
        Page<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoPage = page.map(hazardousChemicalsTotalMapper::toDto);
        long pageSize = page.getTotalElements();
        List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalDtoPage.getContent();
        List<HazardousChemicalsTotalDto> newhazardousChemicalsTotalDtoList = new ArrayList<>();
        if (hazardousChemicalsTotalDtoList.size() == 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("content", newhazardousChemicalsTotalDtoList);
            map.put("totalElements", pageSize);
            return map;
        }
        newhazardousChemicalsTotalDtoList.add(hazardousChemicalsTotalDtoList.get(0));
        int j = 0;
        for (int i = 1; i < hazardousChemicalsTotalDtoList.size(); i++) {
            if (hazardousChemicalsTotalDtoList.get(i).getName().equals(hazardousChemicalsTotalDtoList.get(i - 1).getName()) && hazardousChemicalsTotalDtoList.get(i).getCabinetId().equals(hazardousChemicalsTotalDtoList.get(i - 1).getCabinetId()) && hazardousChemicalsTotalDtoList.get(i).getExpirationDate().equals(hazardousChemicalsTotalDtoList.get(i - 1).getExpirationDate())) {
                newhazardousChemicalsTotalDtoList.get(j).setTotalNumber(newhazardousChemicalsTotalDtoList.get(j).getTotalNumber() + hazardousChemicalsTotalDtoList.get(i).getTotalNumber());
                pageSize--;
            } else {
                newhazardousChemicalsTotalDtoList.add(hazardousChemicalsTotalDtoList.get(i));
                j++;
            }
        }
        for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : newhazardousChemicalsTotalDtoList) {

            Integer bottleNum = hazardousChemicalsRepository.countBottle(hazardousChemicalsTotalDto.getName());
            hazardousChemicalsTotalDto.setBottle(bottleNum);
            Cabinet cabinet = cabinetRepository.getById(hazardousChemicalsTotalDto.getCabinetId());
            hazardousChemicalsTotalDto.setCabinetName(cabinet.getName());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("content", newhazardousChemicalsTotalDtoList);
        map.put("totalElements", pageSize);
        return map;
    }


    @Override
    public Map<String, Object> queryList() {
        List<HazardousChemicalsTotal> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllByNullList();
        List<HazardousChemicalsTotalDto> hazardousChemicalsTotalDtoList = hazardousChemicalsTotalMapper.toDto(hazardousChemicalsTotalList);
        for (HazardousChemicalsTotalDto hazardousChemicalsTotalDto : hazardousChemicalsTotalDtoList) {
            if (hazardousChemicalsTotalDto.getCountType() == 1) {
                hazardousChemicalsTotalDto.setNameAmount(hazardousChemicalsTotalDto.getName().concat(" ").concat(String.valueOf(hazardousChemicalsTotalDto.getAmount())).concat("(ml)"));
            } else {
                hazardousChemicalsTotalDto.setNameAmount(hazardousChemicalsTotalDto.getName().concat(" ").concat(String.valueOf(hazardousChemicalsTotalDto.getAmount())).concat("(g)"));
            }
        }
        Map<String, Object> stringObjectMap = new HashMap<>();
        stringObjectMap.put("data", hazardousChemicalsTotalDtoList);
        return stringObjectMap;
    }


    public List<Map<String, Object>> exportHazardousChemicalsTotal(HazardousChemicalsTotalQueryCriteria hazardousChemicalsTotalQueryCriteria) {

        if (hazardousChemicalsTotalQueryCriteria.getName() == null && hazardousChemicalsTotalQueryCriteria.getCabinetId() == null) {
            List<Map<String, Object>> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryAllForDownload();

            return hazardousChemicalsTotalList;
        }

        if (hazardousChemicalsTotalQueryCriteria.getName() != null && hazardousChemicalsTotalQueryCriteria.getCabinetId() == null) {
            List<Map<String, Object>> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryForDownloadByName(hazardousChemicalsTotalQueryCriteria.getName());

            return hazardousChemicalsTotalList;
        }

        if (hazardousChemicalsTotalQueryCriteria.getName() == null && hazardousChemicalsTotalQueryCriteria.getCabinetId() != null) {
            List<Map<String, Object>> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryForDownloadByCabinetId(hazardousChemicalsTotalQueryCriteria.getCabinetId());

            return hazardousChemicalsTotalList;
        }

        if (hazardousChemicalsTotalQueryCriteria.getName() != null && hazardousChemicalsTotalQueryCriteria.getCabinetId() != null) {
            List<Map<String, Object>> hazardousChemicalsTotalList = hazardousChemicalsTotalRepository.queryForDownloadByCabinetIdAndName(hazardousChemicalsTotalQueryCriteria.getCabinetId(), hazardousChemicalsTotalQueryCriteria.getName());

            return hazardousChemicalsTotalList;
        }

        return null;
    }

    public static List<Integer> getDiffrent(List<Integer> list1, List<Integer> list2) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>(list1.size() + list2.size());
        List<Integer> diff = new ArrayList<Integer>();
        List<Integer> maxList = list1;
        List<Integer> minList = list2;
        if (list2.size() > list1.size()) {
            maxList = list2;
            minList = list1;
        }

        for (Integer string : maxList) {
            map.put(string, 1);
        }

        for (Integer string : minList) {
            Integer cc = map.get(string);
            if (cc != null) {
                map.put(string, ++cc);
                continue;
            }
            map.put(string, 1);
        }

        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                diff.add(entry.getKey());
            }
        }
        return diff;
    }


}
